"""
大模型测试

requests包：用来http请求调用
json: json工具
datetime:时间计算
"""
import sys

import requests
import json
from datetime import datetime


# 前置方法
def pre_intent(url: str, prompt: str):
    # j将字段数据转json格式的数据
    pyload = json.dumps({
        "vin": "LSJA24396BS11003",
        "userId": "dev_user_1",
        "queryId": "dev_d187c18ae79a74d873527cd9a16e67b0",
        "voiceZone": 1,
        "text": prompt,
        "lat": 31.18,
        "lon": 121.6,
        "timestamp": 1698298943699
    })
    headers = {
        'Content-Type': 'application/json'
    }
    response = requests.request("POST", url, headers=headers, data=pyload)
    return response.json()


# 获取job_ID
def get_job_id(url: str, prompt: str) -> str:
    payload = json.dumps({
        "request_id": "900000000656777",
        "session_id": "LSJWR4096PS083548169511788336",
        "timestamp": "1695117927776",
        "user_id": "9000000006567824",
        "prompt": prompt,
        "model": "chatglm_66b_test01",
        "params": {"temperature": 0.9},
        "context": {
            "lat": 31.183197,
            "lon": 121.605395
        },
        "character": "",
        "knowlege_base": True,
        "sid": "123456 ",
        "version": "20231030",
        "queryId": "dev_d187c18ae79a74d873527cd9a16e67b0"

    })
    headers = {
        'Content-Type': 'application/json'
    }
    response = requests.request("POST", url, headers=headers, data=payload)
    return response.json()


# 获取聊天内容
def get_content(job_id: str, version: str, url: str):
    payload = json.dumps({
        "job_id": job_id,
        "version": version
    })
    headers = {
        'Content-Type': 'application/json'
    }
    response = requests.request("POST", url, headers=headers, data=payload)
    return response.json()


print("------------------------------------------")
vass_sit = "http://vass-sit.immotors.com/vass-app"
vass_uat = "http://vass-uat.immotors.com/vass-app"

model_sit = "http://10.184.44.30:8081/everCalcWeb/v1/im-model/intent-recognition"
model_uat = "http://ever-calc-api-uat.immotors.com/everCalcWeb/v1/im-model/intent-recognition "

vass_url = vass_sit
model_url = model_sit
prompt = "画一张蒙娜丽莎的图片"
version = "20231109"

pre_intent(model_url, prompt)  # 前置条件
response = get_job_id(vass_url + "/multi-llm/create-chat", prompt)  # 获取jobId
job_id = response["data"]["job_id"]
print(job_id)
content = get_content(job_id, version=version, url=vass_url + "/multi-llm/get-chat-record")
print(f"第一次响应结果:{content}")

success_code = content["code"]
if "200" != success_code:
    print(f"错误结果:{content}")
    sys.exit("222")

current_time = datetime.now()
milliseconds_time = current_time.microsecond // 1000  # 将为微妙转化为毫秒
print(f"开始时间:{current_time}")

# 获取status
status = content["data"]["status"]
while status == "PROCESSING":
    content = get_content(job_id, version="20231109", url=vass_url + "/multi-llm/get-chat-record")
    print(f"打印响应结果:{content}")
    if content["code"] != "200":
        current_time = datetime.now()
        print(f"结束时间:{current_time}")
        break

    status = content["data"]["status"]
    if status == "FINISHED":  # 如果状态等于 FINISHED 则结束
        image = content["data"]["image"]
        if image is not None:
            urls = image["urls"]
            print(f"图片的地址:{urls}")

        break

